//
//  ByCellServiceGoodListItem.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/11/10.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellServiceGoodListItem.h"

@implementation ByCellServiceGoodListItem
{
    UIView          *_bgView;
    
    UIImageView     *_mainView;         // 主图
    
    UILabel         *_nameLabel;        // 名称
    UILabel         *_receommendLabel;  // 推荐
    
    CAShapeLayer    *_dottedLayer;      // 虚线
    
    
    UIView          *_tagsView;         // 标签
    UILabel         *_priceLabel;       // 现价
    UILabel         *_originPriceLabel; // 原价

    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    _bgView = [UIImageView new];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    _mainView = [UIImageView new];
    _mainView.image = [UIImage imageNamed:@"头视图背景"];
    [_bgView addSubview:_mainView];

    _nameLabel = [[UILabel alloc] init];
    _nameLabel.text = @"婚庆一条龙";
    _nameLabel.font = FONT(16);
    _nameLabel.textAlignment = NSTextAlignmentLeft;
    _nameLabel.textColor = [UIColor blackColor];
    [_bgView addSubview:_nameLabel];
    
    _receommendLabel = [[UILabel alloc] init];
    _receommendLabel.text = @"推荐";
    _receommendLabel.font = FONT(14);
    _receommendLabel.textAlignment = NSTextAlignmentCenter;
    _receommendLabel.textColor = BY_RED_COLOR;
    _receommendLabel.layer.borderColor = BY_RED_COLOR.CGColor;
    _receommendLabel.layer.borderWidth = 1;
    [_bgView addSubview:_receommendLabel];
    
    _priceLabel = [[UILabel alloc] init];
    _priceLabel.text = @"￥88888.88";
    _priceLabel.font = FONT(18);
    _priceLabel.textAlignment = NSTextAlignmentLeft;
    _priceLabel.textColor = BY_RED_COLOR;
    [_bgView addSubview:_priceLabel];
    
    _originPriceLabel = [[UILabel alloc] init];
    _originPriceLabel.text = @"￥99999.99";
    _originPriceLabel.font = FONT(14);
    _originPriceLabel.textAlignment = NSTextAlignmentRight;
    _originPriceLabel.textColor = [UIColor lightGrayColor];
    [_bgView addSubview:_originPriceLabel];
    
    _dottedLayer = [CAShapeLayer layer];
    _dottedLayer.lineDashPattern = @[@3, @2];
    _dottedLayer.lineWidth = 1;
    _dottedLayer.strokeColor = [UIColor colorWithWhite:0.8 alpha:1].CGColor;
    [_bgView.layer addSublayer:_dottedLayer];
    
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 10, 10, 10));
    }];
    // 布局
    [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(@0);
        make.height.equalTo(_mainView.mas_width).multipliedBy(0.5);
    }];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(_mainView.mas_bottom);
        make.height.equalTo(@50);
        make.right.equalTo(_receommendLabel.mas_left).offset(-10);
    }];
    [_receommendLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-10);
        make.size.mas_equalTo(CGSizeMake(36, 20));
        make.centerY.equalTo(_nameLabel);
    }];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel);
        make.top.equalTo(_nameLabel.mas_bottom);
        make.height.equalTo(@36);
    }];
    [_originPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-10);
        make.top.height.equalTo(_priceLabel);
    }];
    
    
    // 现价
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_priceLabel.text];
    [attrStr addAttribute:NSFontAttributeName value:FONT(14) range:[_priceLabel.text rangeOfString:@"￥"]];
    [attrStr addAttribute:NSFontAttributeName value:FONT(14) range:NSMakeRange(_priceLabel.text.length - 2, 2)];
    _priceLabel.attributedText = attrStr;
    // 原价
    attrStr = [[NSMutableAttributedString alloc] initWithString:_originPriceLabel.text];
    [attrStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid|NSUnderlineStyleSingle) range:[_originPriceLabel.text rangeOfString:_originPriceLabel.text]];
    _originPriceLabel.attributedText = attrStr;
}


- (void)layoutSubviews {
    UIBezierPath *dottedPath = [UIBezierPath bezierPath];
    [dottedPath moveToPoint:CGPointMake(10, self.height - 10 - 36)];
    [dottedPath addLineToPoint:CGPointMake(self.width - 20 - 10, self.height - 10 - 36)];
    _dottedLayer.path = dottedPath.CGPath;
}


@end
